import React, {Component} from "react";
import {
    StyleSheet,
    View,
    Animated,
    Image,
    Dimensions,
    BackHandler,
    ToastAndroid,
    StatusBar,
    findNodeHandle,
    UIManager,
    TouchableOpacity,
} from "react-native";
import {StackNavigator, TabNavigator, TabBarBottom} from "react-navigation";
import CardStackStyleInterpolator from 'react-navigation/src/views/CardStack/CardStackStyleInterpolator'
import SplashScene from "./SplashScene";
import GuideScene from "./GuideScene";
import SelectScene from "./select/SelectScene";
import CourseScene from "./course/CourseScene";
import MineScene from "./mine/MineScene";
import color from "../widget/color";
import TabBarItem from "../widget/TabBarItem";
import CourseDetailScene from "./detail/CourseDetailScene";
import CalendarSecene from "./select/CalendarSecene";
import CourseOrderScene from "./detail/CourseOrderScene";
import UserinfoPage from "./mine/personalinfo/UserinfoPage";
import PersonalMateralScene from "./mine/personalinfo/PersonalMateralScene";
import VideoScene from './video/VideoScene'
import MineOrderScene from "./mine/order/MineOrderScene";
import MineUnPayDetailScene from "./mine/order/MineUnPayDetailScene";
import AddressScene from './mine/address/AddressScene'
import MineCancelDetailScene from "./mine/order/MineCancelDetailScene";
import MinePayDetailScene from "./mine/order/MinePayDetailScene";
import MineAddressScene from "./mine/address/MineAddressScene";
import CommentListScene from "./comment/CommentListScene";
import CommentScene from "./comment/CommentScene";
import MineCourseDetailScene from "./course/MineCourseDetailScene";
import DialogScene from "./DialogScene";
import MineInfoFeekback from "./mine/feedback/MineInfoFeekback";
import MineSettingScene from "./mine/settings/MineSettingScene";
import MyHomeworkScene from "./mine/homework/MyHomeworkScene";
import LessonListScene from "./mine/homework/LessonListScene";
import SubmitHomeworkScene from "./mine/homework/SubmitHomeworkScene";
import ImageShowScene from "./mine/homework/ImageShowScene";
import AddAddressScene from "./mine/address/AddAddressScene";
import EditAddressScene from "./mine/address/EditAddressScene";
import LoginScene from "./mine/login/LoginScene";
import LoginSmsScene from "./mine/login/LoginSmsScene";
import ResigterScene from "./mine/login/ResigterScene";
import LoginSubjectScene from "./mine/login/LoginSubjectScene";
import EditPwdScene from "./mine/login/EditPwdScene";
import SearchScene from "./search/SearchScene";
import SearchResultScene from "./search/SearchResultScene";
import TeacherHomeScene from "./search/TeacherHomeScene";
import LoginSexScene from "./mine/login/LoginSexScene";
import CourseVideoScene from "./video/CourseVideoScene";
import AlteredDetailScene from "./mine/homework/AlteredDetailScene";
import AboutUsScene from "./mine/aboutus/AboutUsScene";
import LoginViewController from "./mine/login/LoginViewController";
import MineCouponScene from "./mine/coupon/MineCouponScene";
import MineUnUseCouponscene from "./mine/order/MineUnUseCouponscene";
import CouponDetailScene from "./mine/coupon/CouponDetailScene";
import IapScene from './mine/iap/IapScene'
import MineRefuesOrderScene from "./mine/order/MineRefuesOrderScene";
import MineRefuesDetailScene from "./mine/order/MineRefuesDetailScene";
import ShoppingScene from "./mine/shopping/ShoppingScene";
import ShoppingCart from "./mine/shopping/ShoppingCart";
import ShoppingOrderList from "./mine/shopping/ShoppingOrderList";
import BuySuccessScene from "./detail/BuySuccessScene";
import ZiliaoCell from "../widget/course/ZiliaoCell";
import ZiLiaoScene from "./course/ZiLiaoScene";
import PDFListScene from "./course/PDFListScene";
import PDFLook from "./course/PDFLook";
type Props = {
    navigation:any,
}

export default class RootScene extends Component<Props> {

    constructor() {
        super()
        StatusBar.setBarStyle('light-content')
    }

    render() {
        return (
            <View style={{flex:1}}>
                <Navigator/>
            </View>
        );
    }

}

const Tab = TabNavigator(
    {
        Home: {
            screen: SelectScene,
            navigationOptions: ({navigation}) => ({
                tabBarLabel: '选课',
                tabBarIcon: ({focused, tintColor}) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('../../res/img/tabbar/unsecles_course.png')}
                        selectedImage={require('../../res/img/tabbar/select_course.png')}
                    />
                )
            }),
        },
        Nearby: {
            screen: CourseScene,
            navigationOptions: ({navigation}) => ({
                tabBarLabel: '我的课程',
                tabBarIcon: ({focused, tintColor}) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('../../res/img/tabbar/my_course_unselect.png')}
                        selectedImage={require('../../res/img/tabbar/my_course_select.png')}
                    />
                )
            }),
        },

        Mine: {
            screen: MineScene,
            navigationOptions: ({navigation}) => ({
                tabBarLabel: '我的',
                tabBarIcon: ({focused, tintColor}) => (
                    <TabBarItem
                        tintColor={tintColor}
                        focused={focused}
                        normalImage={require('../../res/img/tabbar/my_unselect.png')}
                        selectedImage={require('../../res/img/tabbar/my_select.png')}
                    />
                )
            }),
        },
    }, {
        tabBarComponent: TabBarBottom,
        // initialRouteName: 'Nearby',
        tabBarPosition: 'bottom',
        lazy: true,
        animationEnabled: false,

        backBehavior: 'none', // 按 back 键是否跳转到第一个 Tab， none 为不跳转
        swipeEnabled: false,
        tabBarOptions: {
            activeTintColor: color.primary,
            inactiveTintColor: color.gray,
            style: {backgroundColor: '#ffffff'},
        },
    }
)

const Navigator = StackNavigator(
    {
        splash: {screen: SplashScene},
        guideScene: {screen: GuideScene},
        LoginViewController: {screen: LoginViewController},
        home: {screen: Tab},
        CourseDetailScene: {screen: CourseDetailScene},
        CourseOrderScene: {screen: CourseOrderScene},
        calendarSecene: {screen: CalendarSecene},
        personInfo: {screen: UserinfoPage},
        personalMateralScene: {screen: PersonalMateralScene},
        videoScene: {screen: VideoScene},
        MineOrderScene: {screen: MineOrderScene},
        MineUnPayDetailScene: {screen: MineUnPayDetailScene},
        MineAddressScene: {screen: MineAddressScene},
        addressScene: {screen: AddressScene},
        MineCancelDetailScene: {screen: MineCancelDetailScene},
        MinePayDetailScene: {screen: MinePayDetailScene},
        CommentListScene: {screen: CommentListScene},
        CommentScene: {screen: CommentScene},
        MineCourseDetailScene: {screen: MineCourseDetailScene},
        dialogScene: {screen: DialogScene},
        MineInfoFeekback: {screen: MineInfoFeekback},
        MineSettingScene: {screen: MineSettingScene},
        myHomeworkScene: {screen: MyHomeworkScene},
        lessonListScene: {screen: LessonListScene},
        submitHomeworkScene: {screen: SubmitHomeworkScene},
        AddAddressScene: {screen: AddAddressScene},
        EditAddressScene: {screen: EditAddressScene},
        LoginScene: {screen: LoginScene},
        imageShowScene: {screen: ImageShowScene},
        ResigterScene: {screen: ResigterScene},
        LoginSubjectScene: {screen: LoginSubjectScene},
        EditPwdScene: {screen: EditPwdScene},
        LoginSexScene: {screen: LoginSexScene},
        CourseVideoScene: {screen: CourseVideoScene},
        alteredDetailScene: {screen: AlteredDetailScene},
        searchScene: {screen: SearchScene},
        searchResultScene: {screen: SearchResultScene},
        aboutUsScene: {screen: AboutUsScene},
        teacherHomeScene: {screen: TeacherHomeScene},
        MineCouponScene:{screen:MineCouponScene},
        MineUnUseCouponscene:{screen:MineUnUseCouponscene},
        couponDetailScene:{screen:CouponDetailScene},
        IapScene:{screen:IapScene},
        MineRefuesOrderScene:{screen:MineRefuesOrderScene},
        MineRefuesDetailScene:{screen:MineRefuesDetailScene},
        ShoppingScene:{screen:ShoppingScene},
        ShoppingCart:{screen:ShoppingCart},
        ShoppingOrderList:{screen:ShoppingOrderList},
        BuySuccessScene:{screen:BuySuccessScene},
        ZiLiaoScene:{screen:ZiLiaoScene},
        PDFListScene:{screen:PDFListScene},
        PDFLook:{screen:PDFLook},
    },
    {
        navigationOptions: ({navigation}) => ({
            headerBackTitle: null,
            showIcon: true,
            headerTitleStyle: {
                fontSize: 20,
                fontWeight: 'bold',
            },
            cardStack: {
                gesturesEnabled: true, //是否可以使用手势关闭此屏幕。在iOS上默认为true，在Android上为false
                // 设置滑动返回的距离
                gestureResponseDistance:{horizontal:300},
            },
        }),
        transitionConfig: (() => ({
            screenInterpolator: CardStackStyleInterpolator.forHorizontal
        }))
    }
)
const defaultGetStateForAction = Navigator.router.getStateForAction;
Navigator.router.getStateForAction = (action, state) => {
    // goBack返回指定页面
    if (state && action.type === 'Navigation/BACK' && action.key) {
        const backRoute = state.routes.find((route) => route.routeName === action.key);
        if (backRoute) {
            const backRouteIndex = state.routes.indexOf(backRoute);
            const purposeState = {
                ...state,
                routes: state.routes.slice(0, backRouteIndex + 1),
                index: backRouteIndex,
            };
            return purposeState;
        }
    }
    return defaultGetStateForAction(action, state)
};
